import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space, Switch } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

// 自动化规则数据结构定义
export interface AutomationRuleField {
  /** 唯一标识 */
  id: number;

  /** 所属模板ID（外键关联template表） */
  templateId: number;

  /** 从设备节点标识 */
  slaveNode: string;

  /** 自动化规则名称 */
  automationName: string;

  /** 触发类型 */
  triggerType: string;

  /** 动作类型 */
  actionType: string;

  /** 生成报警信息选项 */
  generateAlarm: string;

  /** 循环触发通知间隔（分钟） */
  loopTriggerInterval: number;

  /** 是否启用此触发器规则 */
  isEnabled: boolean;

  /** 报警恢复触发通知选项 */
  alarmRecoveryTrigger?: string;

  /** 记录创建时间 */
  createdAt?: string | Date;

  /** 记录最后更新时间 */
  updatedAt?: string | Date;
}

export const createAutomationRuleFieldField = (
  tableAction: TableAction<AutomationRuleField> = {}
): Fields[] => {
  return [
    {
      valueType: "number",
      inputType: "input",
      key: "id",
      title: "ID",
      disabled: true,
      dataIndex: "id",
      width: 80,
      isEdit: false,
      visible: true,
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "模板ID",
      dataIndex: "templateId",
      key: "templateId",
      placeholder: "请输入所属模板ID",
      rules: [
        { required: true, message: "模板ID不能为空" },
        { type: "number", min: 1, message: "模板ID必须大于0" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "从设备节点标识",
      dataIndex: "slaveNode",
      key: "slaveNode",
      placeholder: "请输入从设备节点标识",
      rules: [
        { required: true, message: "从设备节点标识不能为空" },
        { max: 255, message: "标识长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "自动化规则名称",
      dataIndex: "automationName",
      key: "automationName",
      placeholder: "请输入自动化规则名称",
      rules: [
        { required: true, message: "自动化规则名称不能为空" },
        { max: 255, message: "名称长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "触发类型",
      dataIndex: "triggerType",
      key: "triggerType",
      placeholder: "请选择触发类型",
      options: [
        { value: "定时触发", title: "定时触发" },
        { value: "条件触发", title: "条件触发" },
        { value: "事件触发", title: "事件触发" },
        // 可根据实际情况添加更多选项
      ],
      rules: [{ required: true, message: "请选择触发类型" }],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "动作类型",
      dataIndex: "actionType",
      key: "actionType",
      placeholder: "请选择动作类型",
      options: [
        { value: "控制设备", title: "控制设备" },
        { value: "公众号通知", title: "公众号通知" },
        { value: "短信通知", title: "短信通知" },
        { value: "邮件通知", title: "邮件通知" },
        // 可根据实际情况添加更多选项
      ],
      rules: [{ required: true, message: "请选择动作类型" }],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "生成报警信息选项",
      dataIndex: "generateAlarm",
      key: "generateAlarm",
      placeholder: "请选择报警信息选项",
      options: [
        { value: "always", title: "总是生成" },
        { value: "never", title: "从不生成" },
        { value: "condition", title: "条件生成" },
        // 可根据实际情况添加更多选项
      ],
      rules: [{ required: true, message: "请选择报警信息选项" }],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "循环触发通知间隔(分钟)",
      dataIndex: "loopTriggerInterval",
      key: "loopTriggerInterval",
      placeholder: "请输入循环触发通知间隔",
      rules: [
        { required: true, message: "循环触发通知间隔不能为空" },
        { type: "number", min: 0, message: "间隔时间必须大于等于0" },
      ],
    },
    {
      valueType: "number",
      inputType: "radio",
      title: "是否启用此触发器规则",
      dataIndex: "isEnabled",
      key: "isEnabled",
      initialValue: 0,
      options: [
        { value: 0, title: "是" },
        { value: 1, title: "否" },
      ],
      rules: [{ required: true, message: "请选择是否启用此规则" }],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "报警恢复触发通知选项",
      dataIndex: "alarmRecoveryTrigger",
      key: "alarmRecoveryTrigger",
      placeholder: "请选择报警恢复触发选项",
      options: [
        { value: "always", title: "总是通知" },
        { value: "never", title: "从不通知" },
        { value: "condition", title: "条件通知" },
        // 可根据实际情况添加更多选项
      ],
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "创建时间",
      dataIndex: "createdAt",
      key: "createdAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "更新时间",
      dataIndex: "updatedAt",
      key: "updatedAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      render: (_: any, record: AutomationRuleField) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除此规则？"
            description="删除规则将无法恢复"
            onConfirm={() => tableAction.delete?.([record.id])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};
